package com.example.dbsample.web;

import cn.hutool.extra.spring.SpringUtil;
import com.example.dbsample.model.entity.SysMenu;
import com.example.dbsample.model.vo.PHAIResponse;
import com.example.dbsample.service.business.SysMenuService;
import com.example.dbsample.web.BaseRest;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * 菜单管理;(sys_menu)表控制层
 * @author : http://www.chiner.pro
 * @date : 2024-3-6
 */
@Api(tags = "菜单管理对象功能接口")
@RestController
@RequestMapping("/sysMenu")
public class SysMenuController extends BaseRest {
    @Autowired
    private SysMenuService sysMenuService;
    
    /** 
     * 通过ID查询单条数据 
     *
     * @param id 主键
     * @return 实例对象
     */
    @ApiOperation("通过ID查询单条数据")
    @GetMapping("{id}")
    public ResponseEntity<SysMenu> queryById(@PathVariable("id") Integer id){
        return ResponseEntity.ok(sysMenuService.queryById(id));
    }

    @ApiOperation("通过NAME查询单条数据")
    @GetMapping("queryByName/{name}")
    public ResponseEntity<List<SysMenu>> queryByName(@PathVariable("name") String name){
        //return ResponseEntity.ok(sysMenuService.queryByName(name));
        return ResponseEntity.ok(sysMenuService.queryByName("系统"));
    }

    /** 
     * 分页查询
     *
     * @param sysMenu 筛选条件
     * @param pageRequest 分页对象
     * @return 查询结果
     */
    @ApiOperation("分页查询")
    @GetMapping
    public ResponseEntity<Page<SysMenu>> paginQuery(SysMenu sysMenu, PageRequest pageRequest){
        return ResponseEntity.ok(sysMenuService.paginQuery(sysMenu, pageRequest));
    }
    
    /** 
     * 新增数据
     *
     * @param sysMenu 实例对象
     * @return 实例对象
     */
    @ApiOperation("新增数据")
    @PostMapping("/add")
    //public ResponseEntity<SysMenu> add(SysMenu sysMenuInput){
    public ResponseEntity<SysMenu> add(){

        SysMenu sysMenu = new SysMenu();

        sysMenu.setId(51);
        sysMenu.setPid(38);
        sysMenu.setName("操作日志1_手动添加的");
        sysMenu.setUrl("sys/log/operate");
        sysMenu.setAuthority("sys:operate:all");
        sysMenu.setType(0);
        sysMenu.setOpenStyle(0);
        sysMenu.setIcon("icon-file-text");
        sysMenu.setSort(1);
        sysMenu.setVersion(0);
        sysMenu.setDeleted(0);
        sysMenu.setCreator(10000);
        sysMenu.setCreateTime(new Date());
        sysMenu.setUpdater(10000);
        sysMenu.setUpdateTime(new Date());

        sysMenuService.insert(sysMenu);

        SysMenu querySysMenu = sysMenuService.queryById(47);
        System.out.println(querySysMenu);


        //return ResponseEntity.ok(sysMenuService.insert(sysMenu));
        return ResponseEntity.ok(querySysMenu);
    }
    
    /** 
     * 更新数据
     *
     * @param sysMenu 实例对象
     * @return 实例对象
     */
    @ApiOperation("更新数据")
    @PutMapping
    public ResponseEntity<SysMenu> edit(SysMenu sysMenu){
        return ResponseEntity.ok(sysMenuService.update(sysMenu));
    }
    
    /** 
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @ApiOperation("通过主键删除数据")
    @DeleteMapping
    public ResponseEntity<Boolean> deleteById(Integer id){
        return ResponseEntity.ok(sysMenuService.deleteById(id));
    }

    /**
     * TODO 待验证
     * @param dataSetName
     * @return
     */
    // 强调：ApiImplicitParams
    @PostMapping
    @ApiOperation("新增数据集")
    //@ApiImplicitParam(name = "数据集名称", value = "dataSetName")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "数据集名称", value = "dataSetName", required = true),
            @ApiImplicitParam(name = "文件Id", value = "fileId")

    })
    public PHAIResponse<Object> insertDataSet(@RequestParam String dataSetName) {

        // 获取配置的值
        String enable = SpringUtil.getProperty("uamp.auth.enable");

        return PHAIResponse.success();
    }
}